package guowei.guliproduct.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import guowei.guliproduct.product.entity.AttrEntity;
import guowei.guliproduct.product.service.AttrService;
import guowei.guliproduct.product.service.CategoryService;
import guowei.guliproduct.product.vo.AttrGroupAndRelations;
import guowei.guliproduct.product.vo.AttrRelationDelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import guowei.guliproduct.product.entity.AttrGroupEntity;
import guowei.guliproduct.product.service.AttrGroupService;
import guowei.common.utils.PageUtils;
import guowei.common.utils.R;



/**
 * 属性分组
 *
 * @author guoguo
 * @email 1573746397@qq.com
 * @date 2023-05-21 17:38:07
 */
@RestController
@RequestMapping("product/attrgroup")
@Slf4j
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    /**
     * 获取分类下所有分组&关联属性
     */
    @RequestMapping("/{catelogId}/withattr")
    public R getGroupAndAttrRelation(@PathVariable("catelogId") Long catelogId){
        ArrayList<AttrGroupAndRelations> list = attrGroupService.selectAttrGroupAndAttrRelations(catelogId);
        return R.ok().put("data",list);
    }

    /**
     * 新增属性关联
     */
    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody AttrRelationDelVo[] attrRelationDelVos){
        attrService.saveRelation(attrRelationDelVos);
        return R.ok();
    }

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R getNoRelation(@PathVariable("attrgroupId") Long attrGroupId,@RequestParam Map<String,Object> params){
        PageUtils page = attrGroupService.selectNoRelation(attrGroupId,params);

        return R.ok().put("page",page);
    }

    /**
     * 获取属性分组的关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        ArrayList<AttrEntity> list = attrGroupService.getAttrAttrGroupTwo(attrgroupId);
//        AttrEntity[] array = list.toArray(new AttrEntity[list.size()]);
        return R.ok().put("data",list);
    }

    /**
     * 列表 根据categoryId查询相关信息 根据传入的key关键字查询
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPageCatelogId(params,catelogId);


        return R.ok().put("page", page);
    }


    /**
     * 信息  并找到查询子属性的父属性路径
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findParentPath(catelogId);

        attrGroup.setCatelogPath(path);
//        log.info("path的值为:{},{},{}", path[0],path[1],path[2]);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    /**
     * 删除属性与分组的关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrRelationDelVo[] attrRelationDelVos){
        attrGroupService.deleteRelation(attrRelationDelVos);
        return R.ok();
    }

}
